﻿define([
    "dojo/_base/declare"
    , "esri/layers/MapImageLayer"
    , "esri/layers/MapImage"
    , "dojo/_base/connect"
], function (declare
    , MapImageLayer, MapImage, ConnectUtils) {
    var MapImageLayer = declare([MapImageLayer], {
        constructor: function (options) {
            this.inherited(arguments, [options]);
            this._map = null; //地图对象

        }
        , _setMap: function (map, svg) {
            var me = this;
            var g = this.inherited(arguments);

            me._map = map;

            if (me._clickHandle) {
                me._clickHandle.remove();
                me._clickHandle = null;
            }
            ;

            me._clickHandle = map.on('click', function (e) {
                me.clickEvent(e);
            });

            return g;
        }
        , _unsetMap: function (map, svg) {
            var me = this;
            me.inherited(arguments);
            me._mapView.removeLayer('imageIdentifyPt');
            if (me._clickHandle) {
                me._clickHandle.remove()
                me._clickHandle = null;
            }

            me._map = null;
        }
        , setData: function (mapView, layer) {
            var me = this;
            me._mapView = mapView;
            var mapUtils = mapView.mapUtils;
            me._mapUtils = mapUtils;
            if (!mapView) {
                return;
            }
            ;
            me._mapView = mapView;
            if (!mapUtils) {
                return;
            }
            ;

            var options = layer.layer || {};
            me._options = options;
            var data = options.data || [];
            this.factorName = this.factorName || options.factorName;

            me.setImage(options);

            if (typeof me._options.callback == 'function') {
                me._options.callback(layer);
            }
            ;

        },
        setImage: function (options) {
            var me = this;
            options.extent = options.extent || options.pictureExtent;
            if (options.url && options.extent) {
                var extent = me._mapUtils.createEsriExtent(me._mapView, options.extent);
                var mi = new MapImage({
                    'extent': extent,
                    'href': options.url
                });
                me.addImage(mi);
            }
            ;
        },
        formatTime: function (time) {
            if (!time) {
                return '';
            }
            var year = time.substr(0, 4);
            var month = time.substr(4, 2);
            var day = time.substr(6, 2);
            var hour = time.substr(8, 2);
            var minute = time.substr(10, 2);
            var second = time.substr(12, 2);
            return year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second + " UTC";
        },
        formatFactor: function (evt) {
            var _esriData = evt._esriData;
            var result = _esriData.result;
            var features = result.catalogItems.features;
            var values = result.properties.Values;
            if (features.length == 1) {
                return evt.value;
            }
            ;
            if (this.factorName) {
                for (var i = 0; i < features.length; i++) {
                    var attr = features[i].attributes
                    attr.value = values[i];
                    var name = attr.Name.toLowerCase();
                    var factorName = this.factorName.toLowerCase();
                    if (name.includes(factorName)) {
                        return attr.value;
                    }
                    ;
                }
                ;
            }
            ;
            return evt.value;
        },
        _isOutExtent: function (mapPoint) {
            var extent = this._options.extent;
            var x = mapPoint.x;
            var y = mapPoint.y;
            if (x < extent.xMin || x > extent.xMax || y < extent.yMin || y > extent.yMax) {
                return true
            }
        },
        clickEvent: function (e) {
            var me = this;
            if (me._isOutExtent(e.mapPoint)) {
                return;
            }
            ;
            if (me._options && me._options.onClick) {
                if (!me._options.layerUrl) {
                    me._options.onClick({
                        mapView: me._mapView,
                        data: {x: e.mapPoint.x, y: e.mapPoint.y},
                        getLocation: function () {
                            return me._mapView.mapUtils.formateLngLat(me._mapView, e.mapPoint);
                        }
                    })
                    ;
                    return;
                }

                me._mapView.removeLayer('imageIdentifyPt');
                me._mapView.closeInfoWindow();


                var time = me.formatTime(me.curTime);
                me._mapUtils.loading();
                me._mapUtils.imageServiceIdentifyTask(me._mapView, me._options.layerUrl, {
                    geometry: me._mapUtils.createPoint(me._mapView, {x: e.mapPoint.x, y: e.mapPoint.y}),
                    returnGeometry: true,
                    startTime: time,
                    endTime: time,
                    callback: function (evt) {
                        me._mapUtils.unloading();
                        evt.mapView = me._mapView;

                        if (!evt.value || evt.value == "NoData") {
                            return;
                        }
                        ;

                        evt.value = me.formatFactor(evt);
                        evt.data = {x: e.mapPoint.x, y: e.mapPoint.y};
                        if (Object.prototype.toString.call(me._options.onClick) == '[object Function]') {
                            me._mapView.addLayer('pointLayer', {
                                layerName: 'imageIdentifyPt',
                                symbol: 'common_small_point',
                                data: [{x: e.mapPoint.x, y: e.mapPoint.y}]
                            });
                            me._options.onClick(evt);
                        }
                        ;

                    },
                    errorCallback: function (err) {
                        me._mapUtils.unloading();
                        console.log(err)
                    }
                });

            }
            ;
        },

        getGraphicLocation: function (mapView, esriEvent) {
            var geometry = esriEvent.mapPoint;
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        }
    });
    return MapImageLayer;
});